package com.rwaste.worker.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rwaste.commonutils.Msg;
import com.rwaste.commonutils.R;
import com.rwaste.worker.entity.BasicInfo;
import com.rwaste.worker.entity.vo.ListQuery;
import com.rwaste.worker.service.BasicInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cx
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/worker/basicInfo")
@Api(tags = "分理员注册")
public class BasicInfoController {
    @Autowired
    private BasicInfoService basicInfoService;

    //分理员信息类
    @ApiOperation(value = "分理员注册")
    @PostMapping("/saveWorker")
    public R saveWorker(@RequestBody BasicInfo basicInfo){
        basicInfo.setIsVip(0);//vip 默认为0;
        basicInfo.setSign("暂无签名");
        basicInfo.setAvatar("https://edu-my101.oss-cn-beijing.aliyuncs.com/share_9f8fc0b4f170f8d18ba5421f5401ce22.png");
        boolean save = basicInfoService.saveWorkerBaseInfo(basicInfo);
        basicInfo.setIsVip(0);
        if (save){
            return R.ok().message("注册成功");
        }
        return R.ok().error().message("注册失败");

    }



    //登录方法
    @ApiOperation(value = "登录")
    @PostMapping("/sqllogin")
    public R sqllogin(@RequestBody BasicInfo basicInfo){
        BasicInfo res =  basicInfoService.findBaseInfo(basicInfo);
        if (res!=null){
            return R.ok().message("登陆成功").data("baseInfo", res);
        }
        return R.error().message("登陆失败");


    }

    //xml文件登录方法
    @PostMapping("/login/{mobile}/{password}")
    public R login(@PathVariable String mobile,@PathVariable String password){

        BasicInfo basicInfo = basicInfoService.findWorkerByPassword(mobile,password);

        String nickname = basicInfo.getNickname();
        return R.ok().data("admin","admin");
    }

    //tonken 判定
    @GetMapping("/info")
    public R info(){
        return R.ok().data("roles","1").data( "name", "admin").data("avatar", "");
    }

    //根据id 查询分理员信息
    @GetMapping("/findInfo/{id}")
    public R findInfoByid(@PathVariable String id){
        BasicInfo basicInfo =  basicInfoService.findWorkerByid(id);
        return R.ok().data("basicInfo", basicInfo);
    }

    //根据id 冻结账户
    @ApiOperation(value = "冻结账户")
    @GetMapping("/freezeWorker/{id}")
    public R freezeWorker(@PathVariable String id){
        boolean result =  basicInfoService.freezeWorkerById(id);
        if(result){
            return R.ok().message("冻结成功");
        }
        return R.error().message("冻结失败");
    }

    //根据id 解冻账户
    @ApiOperation(value = "解冻账户")
    @GetMapping("/thawWorker/{id}")
    public R thawWorker(@PathVariable String id){
        boolean result =  basicInfoService.thawWorkerById(id);
        if(result){
            return R.ok().message("解冻成功");
        }
        return R.error().message("解冻失败");
    }

    //worker动态分页查询
    @ApiOperation(value = "动态分页查询")
    @PostMapping("/getPage")
    public R getPage( @RequestBody ListQuery listQuery){
        String id = listQuery.getId();
        Integer isVip = listQuery.getIsVip();
        String mobile = listQuery.getMobile();
        Integer page = listQuery.getPage();
        Integer limit = listQuery.getLimit();

        Page<BasicInfo> basicInfoPage =new Page<>(page,limit);

        QueryWrapper<BasicInfo> wrapper = new QueryWrapper<>();

        wrapper.eq("is_deleted", 0);

        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id", id);
        }if (!StringUtils.isEmpty(mobile)){
            wrapper.eq("mobile", mobile);
        }if(!StringUtils.isEmpty(isVip)){
            wrapper.eq("is_vip", isVip);
        }
        basicInfoService.page(basicInfoPage,wrapper);
        long total = basicInfoPage.getTotal();//总数
        List<BasicInfo> records = basicInfoPage.getRecords();



        return R.ok().data("list", records).data("total", total);
    }

    //逻辑删除

    @ApiOperation(value = "删除")
    @GetMapping("/delWorker/{id}")
    public R delWorker(@PathVariable String id){
        boolean result = basicInfoService.delWorker(id);
        if (result){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }
}

